package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.camera.Camera;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.net.UpLoadFiles;
import com.zdsoft.blp.gongyong.upLoad.Base64ToString;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.FBgvAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/3.
 */
public class FBTYActivity extends BaseActivity implements NetCallback{
    private FanHuiView fh_title;
    private GridView gv;
    private ImageView iv_xiangji;
    private List<Bitmap> list;
    private FBgvAdapter adapter;
    private String aSource;
    private EditText et_title,et_contact,et_phone,et_content;
    private String ftitle,fcontact,fphone,fcontent;
    private TextView tv_zfabu;
    private Net net;
    private Map<String,Object> map;
    @Override
    protected void init() {
        setContentView(R.layout.linli_lvyouzhaopin_fb);
        fh_title = find(R.id.fh_title);
        gv=find(R.id.gv);
        et_title=find(R.id.et_title);
        et_contact=find(R.id.et_contact);
        et_phone=find(R.id.et_phone);
        et_content=find(R.id.et_content);
        tv_zfabu=find(R.id.tv_zfabu);

        //设置标题为发消息
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
         aSource=intent.getStringExtra("aSource");
        fh_title.getTv_title().setText(title);
        iv_xiangji=find(R.id.iv_xiangji);

        list = new ArrayList<>();
        adapter = new FBgvAdapter(list, getApplicationContext());
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.size()==position){
                    Camera.setOnClickListener(iv_xiangji, FBTYActivity.this);
                }
            }
        });

        tv_zfabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ftitle=et_title.getText().toString();
                fcontact=et_contact.getText().toString();
                fphone=et_phone.getText().toString();
                fcontent=et_content.getText().toString();
                if (!ftitle.equals("")&&!fcontact.equals("")&&!fphone.equals("")&&!fcontent.equals("")){
                    UpLoadFiles.upLoadFiles(list, handler, null);
                }else {
                    Toast.makeText(FBTYActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                }

            }
        });

    }
    private void getNet(String picId) {

        switch (aSource){
            case "自助旅游":
                net=new Net();
                net.url= API.LYINFO_URL;
                net.nameSpace=API.LYINFO_NAMESPACE;
                net.methodName=API.LYINFOSAVE ;
                map=new HashMap<>();

                break;
            case "招聘":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
                net.methodName=API.SEARCHJOBSAVE ;
                map=new HashMap<>();
                map.put("infoType","zp");
                break;
            case "求职":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
                net.methodName=API.SEARCHJOBSAVE ;
                map=new HashMap<>();
                map.put("infoType","qz");
                break;
            case "寻物":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL_XUNWU;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE_XUNWU;
                net.methodName=API.FOUNTLOSTSAVE ;
                map=new HashMap<>();
                map.put("infoType","xw");
                break;
            case "招领":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL_XUNWU;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE_XUNWU;
                net.methodName=API.FOUNTLOSTSAVE ;
                map=new HashMap<>();
                map.put("infoType","zl");
                break;
            case "出租":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
                net.methodName=API.HOUSERENTALSAVE ;
                map=new HashMap<>();
                map.put("infoType","cz");
                break;
            case "求租":
                net=new Net();
                net.url= API.INFOEXCHANGE_URL;
                net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
                net.methodName=API.HOUSERENTALSAVE ;
                map=new HashMap<>();
                map.put("infoType","cs");
                break;


        }
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId","");

        map.put("id","");
        map.put("title",ftitle);
        map.put("content",fcontent);
        map.put("contact",fcontact);
        map.put("contactPhone",fphone);
        map.put("comIds","");
        map.put("tempFileIds",picId);
        map.put("userId",userId);
        net.parms=map;
        getServer(net,this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Camera.dialog.dismiss();
        switch (requestCode) {
            case Camera.PAI:
                if (resultCode != RESULT_CANCELED) {
                    try {
                        FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/image.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(in);
                        list.add(bitmap);
                        adapter.notifyDataSetChanged();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case Camera.XUAN:
                if (resultCode != RESULT_CANCELED) {
                    ContentResolver resolver = getContentResolver();
                    InputStream in = null;
                    try {
                        in = resolver.openInputStream(data.getData());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    list.add(bitmap);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            try {
                JSONObject object=new JSONObject(res);
                String msg=object.getString("msg");
                Toast.makeText(FBTYActivity.this,msg,Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                if (!"".equals(msg.obj.toString())) {
                    getNet(msg.obj.toString());
                }
            }
        }
    };


}
